/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.asl.BaseController;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Create by xxx on 12/06/2016
 * Controller - 评论
 */
@Controller("appReviewController")
@RequestMapping("/app/m/review")
public class ReviewControl extends BaseController {

    @Resource(name = "reviewServiceImpl")
    private ReviewService reviewService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "courseTradeServiceImpl")
    private CourseTradeService courseTradeService;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;

    /**
     * 添加评论
     * @param review
     * @param courseTradeId(订单id)
     * @return
     */
    @RequestMapping(value = "/add_comment", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage addComment(Review review, Long courseTradeId) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            CourseTrade courseTrade = courseTradeService.find(courseTradeId);
            if (courseTrade == null) {
                return JsonMessage.error("找不到该订单");
            }
            Course course = courseService.find(courseTrade.getCourseId());
            if (course == null) {
                return JsonMessage.error("课程已下架");
            }
            if (review.getFeedbackScore() == null) {
                return JsonMessage.error("真实情况反馈分不能为空");
            }
            if (review.getServiceScore() == null) {
                return JsonMessage.error("服务分不能为空");
            }
            if (review.getSchemeScore() == null) {
                return JsonMessage.error("方案分不能为空");
            }
            courseTrade.setHasReview(true);
            courseTradeService.update(courseTrade);
            Member teacher = memberService.find(courseTrade.getTeacherId());
            if (teacher != null) {
                review.setTeacher(teacher);
            }
            review.setMember(member);
            review.setCourse(course);
            reviewService.save(review);
            return JsonMessage.success("评价成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 我的评论
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/my_comments", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage myComments(Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<Review> page = reviewService.myCommemtPage(pageable, member);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "serviceScore","schemeScore", "feedbackScore", "id", "createDateF", "teacher.name", "content"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}